package com.huohuo.javabasic.thread;

/**
 *
 */
public class Xiancheng {
        public static void main(String[] args) {
            ThreadB b = new ThreadB();
            ThreadA a = new ThreadA(b);
            a.start();
            ThreadC c = new ThreadC(b);
            c.start();
          /*  try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                e.printStackTrace();

            }*/

        }


        /**
         * 线程A
         */
        public static class ThreadA extends Thread{
            private ThreadB b;
            public ThreadA(ThreadB b){
                this.b = b;
            }

            @Override
            public void run() {
                System.out.println("A线程中run方法开始运行了========aaaaaaa=====");
                synchronized (b){
                    try {
                        System.out.println("A线程中run方法,同步代码块开始运行了");
                        b.start();//b
                        Thread.sleep(6000);
                        System.out.println("A线程在执行，线程C要等待6秒才能获得对象锁，执行threadBSync（）方法");
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }

                }

            }


        }

        /**
         * 线程B
         */
        public static class ThreadB extends Thread{
            @Override
            public void run() {
                //try {
                    System.out.println("B线程中run方法在执行了==bbbbbbb===");
//                    Thread.sleep(5000);
//                    System.out.println("A线程调用了B线程后睡眠了4秒");
//                } catch (InterruptedException e) {
//                    e.printStackTrace();
//                }
            }

            //同步方法
            public synchronized void threadBSync(){
                System.out.println("A线程释放了对象锁，C线程调用了线程B中同步方法，调用时间："+System.currentTimeMillis());

            }
        }

        public static class ThreadC extends Thread{

            private ThreadB b;

            public ThreadC(ThreadB b){
                this.b = b;
            }

            @Override
            public void run() {
                System.out.println("C线程需要等待获得线程B的对象锁，才能调用了B线程中的同步方法");
                b.threadBSync();

            }
        }

    }

